package com.dean.ui.panel.impl;

import com.dean.action.PdfMergeAction;
import com.dean.entity.DeanResponse;
import com.dean.ui.panel.DeanPanel;
import com.dean.ui.utils.UiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 数据库管理工具
 * @Author dean
 */
public class PdfMergePanel implements DeanPanel{

	private static final Logger logger = LoggerFactory.getLogger(PdfMergePanel.class);

    /**
     * 表头
     */
    private final String [] heads = {"CHECK_FLAG","FILE_NAME","FILE_PATH"};

    /**
     * 文件存放地址
     */
    private  JLabel sourcePathLabel;
    private  JTextField sourcePath;

    /**
     * 文件生成地址
     */
    private  JLabel targetPathLabel;
    private  JTextField targetPath;

    private  JButton pdfMergeBtn;

	private  JPanel panel = null;
    @Override
	public  JPanel showView(){
		// 初始化 panel
		panel = new JPanel();
		//添加新的panel
		panel.setBounds(UiUtils.menuWidth, 0, UiUtils.contentWidth,UiUtils.noBorderHeight);
		panel.setBorder(BorderFactory.createLineBorder(Color.darkGray,1));
		panel.setLayout(null);

        // 查询配置
        pdfMergeBtn = new JButton("pdf合并");
        pdfMergeBtn.setBounds(UiUtils.elePadding, (UiUtils.inputPadding*3+UiUtils.menuItemHeight*2), UiUtils.labelWidth, UiUtils.menuItemHeight);
        panel.add(pdfMergeBtn);
        pdfMergeBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pdfMerge();
            }
        });
        rightUi();
        return panel;
	}

    @Override
    public JScrollPane showScrollView() {
        return null;
    }

    private void rightUi(){
        // 右边ui布局
        sourcePathLabel = new JLabel("合并文件存放地址:");
        sourcePathLabel.setBounds(UiUtils.inputPadding, UiUtils.inputPadding, UiUtils.labelWidth, UiUtils.menuItemHeight);
        panel.add(sourcePathLabel);

        sourcePath = new JTextField();
        sourcePath.setBounds(UiUtils.inputPadding*2+UiUtils.labelWidth, UiUtils.inputPadding, UiUtils.contentWidth-(UiUtils.inputPadding*3+UiUtils.labelWidth), UiUtils.menuItemHeight);
        panel.add(sourcePath);
        sourcePath.setText("/dean/dean_tool/test/pdf");

        targetPathLabel = new JLabel("合并文件生成地址:");
        targetPathLabel.setBounds((UiUtils.inputPadding), (UiUtils.inputPadding*2+UiUtils.menuItemHeight), UiUtils.labelWidth, UiUtils.menuItemHeight);
        panel.add(targetPathLabel);

        targetPath = new JTextField();
        targetPath.setBounds(UiUtils.inputPadding*2+UiUtils.labelWidth, (UiUtils.inputPadding*2+UiUtils.menuItemHeight), UiUtils.contentWidth-(UiUtils.inputPadding*3+UiUtils.labelWidth), UiUtils.menuItemHeight);
        targetPath.setText("/dean/dean_tool/test/pdf/dean_merge.pdf");
        panel.add(targetPath);
    }

    private void pdfMerge(){
        PdfMergeAction pdfMergeAction = PdfMergeAction.getInstance();
        DeanResponse deanResponse = pdfMergeAction.merge(sourcePath.getText(),targetPath.getText());
        if(deanResponse.getCode() == 0){
            JOptionPane.showMessageDialog(panel, deanResponse.getMsg(), "SUCCESS", JOptionPane.INFORMATION_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(panel, deanResponse.getMsg(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }
}
